<?php
/*
 * $Author ：PHPYUN开发团队
 *
 * 官网: http://www.phpyun.com
 *
 * 版权所有 2009-2019 宿迁鑫潮信息技术有限公司，并保留所有权利。
 *
 * 软件声明：未经授权前提下，不得用于商业运营、二次开发以及任何形式的再次发布。
 */

class chat_controller extends common{
    function index_action()
    {
        if (!$this->uid || !$this->usertype){
            header('location:'.Url('wap',array('c'=>'login')));
        }
        if ($this->config['sy_chat_open']==1){
            
            if (isset($_GET['id']) && isset($_GET['type']) && $this->uid && $this->usertype && $this->usertype != 4){
                
                $uid    =  intval($_GET['id']);
                
                $chatM  =  $this -> MODEL('chat');
                
                $chat   =  $chatM->userinfo(array('uid'=>$uid,'usertype'=>intval($_GET['type']),'nowid'=>$this->uid,'nowtype'=>$this->usertype));
                
                $this -> yunset('receive',$chat['mine']);
                
                $chatM -> upChatLog(array('status'=>1),array('from'=>$uid,'to'=>$this->uid,'fusertype'=>$chat['mine']['usertype'],'tusertype'=>$this->usertype));
                
                $data['nickname']  =  $chat['mine']['username'];
                
                $this->data  =  $data;
                $this -> seo('chat');
                
                if ($this->usertype == 2){
                    
                    $jobM  =  $this->MODEL('job');
                    
                    $num   =  $jobM->getYqmsNum(array('uid'=>$uid,'fid'=>$this->uid));
                    
                    $this -> yunset('inviteNum',$num);
                }
            }
            $this -> yuntpl(array('wap/chat/wapim_yunliao'));
        }
    }
    
    function goChat_action()
    {
        
        if ($this -> config['sy_chat_open'] == 1 && !empty($this->uid) && !empty($this->usertype)){
            
            $chatM  =  $this->MODEL('chat');
            $chat   =  $chatM->userinfo(array('uid'=>$this->uid,'usertype'=>$this->usertype,'history'=>1));
            $token  =  $chatM->getToken($this->uid);
            
            $chat['yuntoken']  =  $token;
            
            echo json_encode($chat);
        }
    }
    function chatList_action()
    {
        if (!$this->uid || !$this->usertype){
            header('location: '.Url('wap',array('c'=>'login')));
        }
        $chatM  =  $this->MODEL('chat');
        $chat   =  $chatM->userinfo(array('uid'=>$this->uid,'usertype'=>$this->usertype,'history'=>1));
        
        $this -> yunset('rows',$chat['history']);
        
        $data['nickname']  =  $this->config['sy_chat_name'].'-'.$this->config['web_name'];
        
        $this->data  =  $data;
        $this -> seo('chat');
        
        $this -> yuntpl(array('wap/chat/yunliao_list'));
    }
    //开始聊天，先发个信息作为开始
    function beginChat_action()
    {
        if ($this->uid && $this->usertype){
            
            $data   =  array(
                'toid'       =>  $this->uid,
                'fromid'     =>  (int)$_POST['id'],
                'tusertype'  =>  $this->usertype,
                'fusertype'  =>  $_POST['usertype'],
                'jobid'      =>  $_POST['jobid'],
                'jobtype'    =>  $_POST['jobtype'],
                'timestamp'  =>  $_POST['timestamp']
            );
            $chatM   =  $this->MODEL('chat');
            
            $return  =  $chatM -> beginChat($data);
            
            echo json_encode($return);
        }
    }
    //添加聊天发送记录
    function chatLog_action()
    {
        $arr     =  array(
            'toid'       =>  $_POST['to'],
            'tusertype'  =>  $_POST['totype'],
            'fromid'     =>  $this->uid,
            'fusertype'  =>  $this->usertype,
            'content'    =>  $_POST['content'],
            'timestamp'  =>  $_POST['timestamp'],
            'msgtype'    =>  $_POST['msgtype'],
            'nowid'      =>  $_POST['nowid'],
            'nowtype'    =>  $_POST['nowtype']
        );
        
        $chatM   =  $this -> MODEL('chat');
        
        $return  =  $chatM -> chatLog($arr);
        
        echo json_encode($return);die;
    }
    /**
     * 聊天记录分页
     */
    function getChatPage_action()
    {
        if ($this->uid && $this->usertype){
            
            $arr    =  array(
                'toid'       =>  $_POST['id'],
                'tusertype'  =>  $_POST['totype'],
                'fromid'     =>  $this->uid,
                'fusertype'  =>  $this->usertype,
                'page'       =>  $_POST['page']
            );
            
            $chatM   =  $this -> MODEL('chat');
            
            $return  =  $chatM -> getChatPage($arr);
            
            echo  json_encode($return);die;
        }
    }
    /**
     * 判断简历是否被下载
     */
    function getdown_action()
    {
        
        $arr   =  array(
            'eid'         =>  $_POST['eid'],
            'toid'        =>  $_POST['toid'],
            'fromid'      =>  $this->uid,
            'fusertype'   =>  $this->usertype,
            'nowtype'     =>  $_POST['nowtype'],
            'zid'         =>  $_POST['zid']
        );
        
        $chatM  =  $this->MODEL('chat');
        
        $return  =  $chatM -> comToChat($arr);
        
        echo json_encode($return);
    }
    /**
     * 发送图片信息
     */
    function uploadImage_action()
    {
        $UploadM  =  $this -> MODEL('upload');
        
        $upArr  =  array(
            'file' =>  $_FILES['file'],
            'dir'  =>  'chat'
        );
        $result  =  $UploadM->newUpload($upArr);
        
        if (!empty($result['msg'])){
            
            $return  =  array(
                'code'  =>  1,
                'msg'   =>  $result['msg'],
                'data'  =>  array()
            );
        }else{
            
            $pic  =  $result['picurl'];
            
            $result['picurl']  =  checkpic($pic);
            
            $return  =  array(
                'code'  =>  0,
                'msg'   =>  '',
                'data'  =>  array('url'=>$result['picurl'])
            );
        }
        echo json_encode($return);
    }
    
    //条件聊天时，判断个人是否有简历，并判断是否申请过该企业职位/已被企业邀请面试
    function isResume_action(){
        
        $arr  =  array(
            'uid'       =>  $this->uid,
            'usertype'  =>  $this->usertype,
            'comid'     =>  $_POST['id'],
            'jobtype'   =>  $_POST['jobtype'],
            'jobid'     =>  $_POST['jobid'],
            'nowtype'   =>  $_POST['nowtype'],
            'zid'       =>  $_POST['zid']
        );
        
        $chatM  =  $this->MODEL('chat');
        
        $return  =  $chatM -> userToChat($arr);
        
        echo json_encode($return);die;
    }
    // 收到消息即设为已读
    function setMsgStatus_action()
    {
        $chatM   =  $this -> MODEL('chat');
        
        $arr     =  array(
            'toid'       =>  $this->uid,
            'fromid'     =>  $_POST['id'],
            'tusertype'  =>  $this->usertype,
            'fusertype'  =>  $_POST['type'],
            'nowid'      =>  $_POST['nowid'],
            'nowtype'    =>  $_POST['nowtype']
        );
        
        $return  =  $chatM->setMsg($arr);
        
        echo json_encode($return);die;
    }
    // 由简历/职位详情页进入,发预定消息
    function prepare_action()
    {
        $pArr   =  array(
            'uid'       =>  $this->uid,
            'usertype'  =>  $this->usertype,
            'toid'      =>  intval($_POST['toid']),
            'totype'    =>  intval($_POST['totype'])
        );
        
        $chatM  =  $this->MODEL('chat');
        
        $list   =  $chatM -> getPrepare($pArr);
        
        echo json_encode($list);die;
    }
    function isLogin_action(){
        
        if ($this->uid && $this->usertype){
            
            $return  =  array('errcode'=>9);
        }else{
            
            $return  =  array('errcode'=>8,'msg'=>'请先登录');
        }
        echo json_encode($return);die;
    }
    // 不感兴趣
    function delChatLog_action()
    {
        $return  =  array();
        
        if ($this->uid && $this->usertype){
            
            $chatM  =  $this -> MODEL('chat');
            
            $chatM->delChatLog(array('toid'=>$_POST['to'],'fromid'=>$this->uid, 'tusertype'=>$_POST['type'],'fusertype'=>$this->usertype));
            
            $chatM->upFriend(array('uid'=>$this->uid,'usertype'=>$this->usertype,'fid'=>$_POST['to'],'fusertype'=>$_POST['type']),array('state'=>2));
            
        }else{
            
            $return  =  array('errcode'=>8,'msg'=>'请先登录');
        }
        echo json_encode($return);die;
    }
    /**
     * 判断聊天套餐情况
     */
    function chatRight_action()
    {
        
        $_POST['uid']       =   $this->uid;
        $_POST['spid']      =   $this->spid;
        $_POST['username']  =   $this->username;
        $_POST['usertype']  =   $this->usertype;
        $comtcM             =   $this->MODEL('comtc');
        $return             =   $comtcM->chatRight($_POST);
        
        if ($return['status']) {
            echo json_encode($return);
        }
    }
    /**
     * 用户不在线，收到消息，提醒用户
     */
    function unSend_action()
    {
        $chatM   =  $this -> MODEL('chat');
        
        $arr     =  array(
            'fromid'     =>  $this->uid,
            'fusertype'  =>  $this->usertype,
            'toid'       =>  $_POST['toid'],
            'tusertype'  =>  $_POST['totype'],
            'nowtype'    =>  $_POST['nowtype']
        );
        
        $return  =  $chatM->setUnSend($arr);
    }
    /**
     * 获取聊天token
     */
    function getToken_action()
    {
        
        $chatM  =  $this->MODEL('chat');
        $token  =  $chatM->getToken($this->uid);
        
        $chat['yuntoken']  =  $token['token'];
        
        echo json_encode($chat);
    }
}
?>